package com.czk.model.proxy.dynamincproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/4/27 21:59
 */
// 代理工厂
public class ProxyFactory {
    // 目标对象
    private Object target;
    public ProxyFactory(Object target){
        this.target=target;
    }
    public Object getProxyInstance(){
        Object obj=Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("jdk代理开始");
                method.invoke(target,args);
                System.out.println("代理结束");
                return proxy;
            }

        });

        return obj;
    }
}
